﻿// 设置权限
var setAuthorize;
// 获取表单数据
var getFormData;
// 设置表单数据
var setFormData;
// 验证数据是否填写完整
var validForm;
var validBusinessData;
// 保存数据
var save;
var saveDraft;
//不可编辑
var setFormDisabled;
var resetUsers;
var resetLocations;
var isUpdate = false;


var keyValue = request('keyValue');
var warnId = request('warnId');
var modelId = request('modelId');

var userdefault = {};
var bootstrap = function ($, learun) {
    "use strict";


    var userinfo;

    // 设置权限
    setAuthorize = function (data, isLook) {
        userinfo = learun.clientdata.get(['userinfo']);
        // 优化滚动条
        //$('.lr-layout-wrap').lrscroll();

        // 保存数据
        $('#savaAndAdd').on('click', function () {
            acceptClick(0);
        });
        $('#save').on('click', function () {
            acceptClick(1);
        });

        //初始化默认值
        $("#createUsername").val(userinfo.user.realName);

        //禁用输入框自动填充
        $("input[type='text']").each(function (index) {
            this.autocomplete = "off";
        });
    };
    //设置表单不可编辑
    setFormDisabled = function (wfType, upload) {
        if (!!wfType && wfType == 100) {
            $('#form').find('input,textarea,select').attr('disabled', 'disabled');
            if (upload != '1') {
                $('#lrUploader_uploadBtn_files').hide();
                $('#lrUploader_uploadBtn_filesList').hide();
            }
            $("#companyId").unbind();
            $("#departmentId").unbind();
        }
    };
    // 获取表单数据
    getFormData = function () {
        var formData = $('#form').lrGetFormData();

        var postData = formData;
        postData.id = parent.processId;
        return postData;
    };
    // 设置表单数据
    setFormData = function (processId, wfType, checkPermission, upload) {
        var title = "告警整改审批流程";
        if(!wfType) {
            $.lrSetForm(config.base_server() + 'auditmodels/findByIds?ids=' + modelId, function (data) {
                if (data != null) {
                    if(data.length == 1) {
                        $("#modelName").val(data[0].codeAndName);
                        title = data[0].codeAndName;
                    } else {
                        var str = "";
                        for(var i=0;i<data.length;i++) {
                            if(i == 0) {
                              str += data[i].codeAndName;
                            } else {
                              str += "、" + data[i].codeAndName;
                            }
                        }
                        $("#modelName").val(str);
                        title = "批量预警整改";
                    }
                }
            });
            $("#modelId").val(modelId);
            $("#warnId").val(warnId);
        }
        if (processId) {
            $('#filesList').lrUploader({isView: false});
            $.lrSetForm(config.base_server() + 'auditmodelwarndeals/findById?id=' + processId, function (data) {
                if (data != null) {
                    isUpdate = true;
                    $('#form').lrSetFormData(data);
                    //$("#processCode").html(data.processCode);
                    modelId = data.modelId;
                    warnId = data.warnId;
                    $("#modelName").val(data.modelName);
                    title = data.modelName;
                } else {
                    $("#print").hide();
                }
                setPbtn(wfType, checkPermission, title);
            });
        } else {
            setPbtn(wfType, checkPermission, title);
        }
        setFormDisabled(wfType, upload);
    };
    // 验证数据是否填写完整
    validForm = function (code) {
        if (!$('#form').lrValidform()) {
            return false;
        }
        // var solution = $("#solution").val();
        // if (!!solution) {
        //     learun.alert.error("请填写整改信息");
        //     return false;
        // }

        return true;
    };
    validBusinessData = function () {
        return true;
    }
    // 保存调用函数
    save = function (processId, force, callBack, i) {
        var keyValue = "";

        if (isUpdate) {
            keyValue = processId;
        }

        var formData = $('#form').lrGetFormData();

        var postData = formData;
        postData.id = parent.processId;

        var postData = formData;

        $.lrSaveForm(config.base_server() + 'auditmodelwarndeals/saveOrUpdate?access_token=' + config.getToken().access_token + '&keyValue=' + keyValue + '&force=' + force, JSON.stringify(postData), function (res) {
            // 保存成功后才回调
            if (!!callBack) {
                callBack(res, i);
            }
        });
    };

    // 保存调用函数
    saveDraft = function (processId, force, callBack, i) {
        var keyValue = "";

        if (isUpdate) {
            keyValue = processId;
        }

        var formData = $('#form').lrGetFormData();

        var postData = formData;
        postData.id = parent.processId;

        var postData = formData;

        $.lrSaveForm(config.base_server() + 'auditmodelwarndeals/saveOrUpdate?access_token=' + config.getToken().access_token + '&keyValue=' + keyValue + '&force=' + force, JSON.stringify(postData), function (res) {
            // 保存成功后才回调
            if (!!callBack) {
                callBack(res, i);
            }
        });
    };


    // 设置父级窗口的按钮
    function setPbtn(wfType, checkPermission, title) {
        if (!!wfType && wfType == 100 && checkPermission == true) { //审核
            // $('#lr-layout-panel-right', window.parent.document).hide();
            $('#flow-force', window.parent.document).hide();
            $('#flow-projectinfo', window.parent.document).hide();
        } else if (!!wfType && wfType == 100 && checkPermission == false) { //查看
            $('#lr-layout-panel-right', window.parent.document).empty();
        } else {
            $('#release', window.parent.document).text('提交申请');
            $('.lr-form-item-title', window.parent.document).text('流程标题');
            $('#savedraft', window.parent.document).hide();
            $('#verifyBill', window.parent.document).hide();
            $('#flow-des', window.parent.document).hide();
            $('#flow-force', window.parent.document).hide();
            $('#sign', window.parent.document).hide();
            if(!!title) {
                $('#flow-title input', window.parent.document).val(title);
            }
            $('#flow-title input', window.parent.document).attr("readonly", "readonly");
            //$('#print', window.parent.document).hide();
        }
    }
}
